package com.jd.fruit.model.admin.shop;


import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jd.fruit.common.annotation.Table;
import com.jd.fruit.common.model.BaseModel;
import com.jd.fruit.common.util.JsonConvertUtil;
import com.jd.fruit.common.util.StringUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 店铺设置
 * @author admin
 *
 */
@SuppressWarnings("serial")
@Table("t_set_shop")
public class SetShop  extends BaseModel<SetShop>{
	public static final SetShop dao=new SetShop();
	
	public Page<SetShop> list(int pageNumber,int pageSize){
		List<Object> whereParams = new ArrayList<Object>();
		StringBuffer whereSql=new StringBuffer(" FROM `t_set_shop`");
		return this.paginate(pageNumber,pageSize,"SELECT  * ",whereSql.toString(),whereParams.toArray());
	}
	
	public List<SetShop>  listshop(){
		String sql="SELECT ts.* FROM t_set_shop ts join t_user tu   on  ts.id=tu.id where 1=1";
		return this.find(sql);	
	}
	public  SetShop  findBy(String id){
		String sql="select * from t_set_shop where id=?";
		return this.findFirst(sql,id);
	}
	public  SetShop  findByIhone(String iphone_no){
		whereParams.clear();
		StringBuffer whereSql=new StringBuffer("SELECT ts.* FROM t_set_shop ts JOIN t_user tu   ON  ts.shop_user_id=tu.id WHERE 1=1 ");
		if(StrKit.notBlank(iphone_no)){
			whereSql.append(" and ts.iphone_no like ?");
			whereParams.add("'%"+iphone_no+"%'");
		}
		return this.findFirst(whereSql.toString(),iphone_no);
	}
	/**
	 * 保存分类属性
	 * @param role
	 * @return 
	 */
	public boolean save(SetShop shop){
		try {
			shop.set("id",StringUtil.getKey());
			return shop.save(); 
		} catch (Exception e) {
			throw new RuntimeException("店铺设置保存异常！", e);
		}
	}
	/**
	 * 设置店铺信息
	 * @param iphone_no
	 * @return
	 */
	public JSONArray findById(String  iphone_no){
		whereParams.clear();
		StringBuffer whereSql=new StringBuffer("SELECT ts.* FROM t_set_shop ts JOIN t_user tu   ON  ts.shop_user_id=tu.id WHERE 1=1 ");
		if(StrKit.notBlank(iphone_no)){
			whereSql.append(" and ts.iphone_no like ?");
			whereParams.add("'%"+iphone_no+"%'");
		}
		//(SetShop) this.find(whereSql.toString(), whereParams.toString())
		List<Record> recordList=Db.find(whereSql.toString(), whereParams.toString());
	    if(recordList.size()>0){
	    	 return JsonConvertUtil.tranformToJSON(recordList, iphone_no);
		}
	    //默认数据
	    JSONArray defaultArray=new JSONArray();
		JSONObject defaultResult=new JSONObject();
		defaultResult.put("data", "NO data!");
		defaultArray.add(defaultResult);
		return defaultArray;
	}
	@Override
	public void initWhere() {
		// TODO Auto-generated method stub
		
	}

}
